/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package its.mx.dsw2.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.Bl.UserBLLocal;
import mx.itsc.ds2.model.User;

/**
 *
 * @author Martinez
 */
@ManagedBean
@SessionScoped
public class UserBean {
    @EJB
    private UserBLLocal userBL;
    private List<User> userList;
    private User user ;
    private String action = "";
    
    
    public UserBean() {
        user = new User();
    }

    public List<User> getUserList() {
        userList = userBL.findAll();
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    
    public void prepareNew(ActionEvent evt) {
        action = "New";
        user = new User();
    }
    
    public String processAction(){
        if (action.equalsIgnoreCase("New")) {
            userBL.register(user);
        }else if(action.equalsIgnoreCase("Edit")){
            userBL.modify(user);
        }else if(action.equalsIgnoreCase("Delete")){
            userBL.remove(user);
        }
        action="";
        return "userList";
    }
    
    public void prepareEdit(ActionEvent evt){
        action = "Edit";
        userList = userBL.findByUser(user);
    }
    
    public void prepareRemove(ActionEvent evt){
        action = "Delete";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        user = userList.get(index);
    }
}
